package com.hzhr.iot.collect.service;

import com.hzhr.iot.collect.constant.entity.FunctionEntity;
import com.hzhr.iot.collect.constant.entity.FunctiontmpEntity;
import com.hzhr.iot.collect.constant.entity.constant.FunctionEntityConstant;
import com.hzhr.iot.collect.constant.entity.constant.FunctiontmpEntityConstant;
import com.hzhr.iot.collect.convertor.custom.CollectLogCustomConvertor;
import com.hzhr.iot.collect.dao.CollectLogDao;
import com.hzhr.iot.collect.entity.CollectLogEntity;
import com.hzhr.iot.collect.entity.ext.CollectLogEntityExt;
import com.hzhr.iot.collect.model.collectLog.CollectLogGetModel;
import com.hzhr.iot.collect.model.collectLog.CollectLogModel;
import com.hzhr.iot.collect.model.collectLog.CollectLogPostModel;
import com.hzhr.iot.collect.queryModel.custom.CollectLogCustomQueryModel;
import com.hzhr.iot.collect.service.base.BaseService;
import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;


@Service
@CygService(entity = CollectLogEntity.class, model = com.hzhr.iot.collect.model.collectLog.CollectLogModel.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
        @CygQuery(name = "_equal_collectDeviceId", query = "equal_collectDeviceId", type = Integer.class),
        @CygQuery(name = "_equal_iotDeviceId", query = "equal_iotDeviceId", type = Integer.class),
        @CygQuery(name = "_equal_iotDeviceCode", query = "equal_iotDeviceCode", type = String.class),
        @CygQuery(name = "_like_iotDeviceCode", query = "like_iotDeviceCode", type = String.class),
        @CygQuery(name = "_equal_functionCode", query = "equal_functionCode", type = String.class),
        @CygQuery(name = "_equal_type", query = "equal_type", type = Integer.class),
        @CygQuery(name = "_equal_starter", query = "equal_starter", type = Integer.class),
        @CygQuery(name = "_greaterthanorequal_startTime", query = "greaterthanorequal_startTime", type = java.util.Date.class),
        @CygQuery(name = "_lessthan_startTime", query = "lessthan_startTime", type = java.util.Date.class)
})
public class CollectLogService extends BaseService {
    @Autowired
    CollectLogCustomConvertor collectLogCustomConvertor;
    @Autowired
    CollectLogDao collectLogDao;
    @Autowired
    CollectLogService collectLogService;
    @Autowired
    Device_IotDeviceService device_iotDeviceService;

    //region javadoc注释 : post
    /**
     * 新增采集数据 <br/><br/>
     * 范围&nbsp;:&nbsp;无 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;新增采集数据 <br/>
     * @param header <br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see CollectLogPostModel body <br/>
     * @see CollectLogGetModel result <br/>
     */ 
    //endregion
    //region 说明 : post
    //新增采集数据
    //范围 : 无
    //步骤 :
    //1. 新增采集数据
    //endregion
    //region 参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/collect-logs", method = RequestMethod.POST)
    //endregion
    public CollectLogGetModel post(Map<String, String> header, Map<String, Object> query,
                                   CollectLogPostModel body) throws ServiceException {
        CollectLogEntity entity = this.collectLogCustomConvertor.collectLogPostModel2entity(body);
        int changeCount = this.collectLogDao.insert(entity);

        CollectLogGetModel result = this.collectLogService.get(initHeader(header), query);
        result.setChangeCount(Integer.valueOf(changeCount));
        return result;
    }

    //region javadoc注释 : get
    /**
     * 查询报警数据 <br/><br/>
     * 范围&nbsp;:&nbsp;无 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询报警数据;&nbsp;(√) <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.id = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_collectDeviceId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.collectDeviceId = #{_equal_collectDeviceId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_iotDeviceId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.iotDeviceId = #{_equal_iotDeviceId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_iotDeviceCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.iotDeviceCode = #{_equal_iotDeviceCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_iotDeviceCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.iotDeviceCode like #{_like_iotDeviceCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_functionCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.functionCode = #{equal_functionCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_starter : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.starter = #{equal_starter};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.type = #{equal_type};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthanorequal_startTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.startTime >= #{greaterthanorequal_startTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _lessthan_startTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where CollectLogModel.startTime < #{lessthan_startTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @throws ServiceException <br/>
     * @see CollectLogGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询报警数据
    //范围 : 无
    //步骤 :
    //1. 查询报警数据; (√)
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where CollectLogModel.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_collectDeviceId", value = "筛选条件 : where CollectLogModel.collectDeviceId = #{equal_collectDeviceId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_iotDeviceId", value = "筛选条件 : where CollectLogModel.iotDeviceId = #{equal_iotDeviceId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_iotDeviceCode", value = "筛选条件 : where CollectLogModel.iotDeviceCode = #{equal_iotDeviceCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_iotDeviceCode", value = "筛选条件 : where CollectLogModel.iotDeviceCode like #{like_iotDeviceCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_functionCode", value = "筛选条件 : where CollectLogModel.functionCode = #{equal_functionCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_starter", value = "筛选条件 : where CollectLogModel.starter = #{equal_starter}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_type", value = "筛选条件 : where CollectLogModel.type = #{equal_type}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_startTime", value = "筛选条件 : where CollectLogModel.startTime >= #{greaterthanorequal_startTime}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lessthan_startTime", value = "筛选条件 : where CollectLogModel.startTime < #{lessthan_startTime}",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where CollectLogModel.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_collectDeviceId", value = "筛选条件 : where CollectLogModel.collectDeviceId = #{_equal_collectDeviceId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_iotDeviceId", value = "筛选条件 : where CollectLogModel.iotDeviceId = #{_equal_iotDeviceId}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_iotDeviceCode", value = "筛选条件 : where CollectLogModel.iotDeviceCode = #{_equal_iotDeviceCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_iotDeviceCode", value = "筛选条件 : where CollectLogModel.iotDeviceCode like #{_like_iotDeviceCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_functionCode", value = "筛选条件 : where CollectLogModel.functionCode = #{equal_functionCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_starter", value = "筛选条件 : where CollectLogModel.starter = #{equal_starter}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_type", value = "筛选条件 : where CollectLogModel.type = #{equal_type}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_startTime", value = "筛选条件 : where CollectLogModel.startTime >= #{greaterthanorequal_startTime}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_lessthan_startTime", value = "筛选条件 : where CollectLogModel.startTime < #{lessthan_startTime}",required = false, dataType = "int", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/collect-logs", method = RequestMethod.GET)
    //endregion
    public CollectLogGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        CollectLogCustomQueryModel queryModel = this.collectLogCustomConvertor.collectLogService_query2queryModel(query);

        List<CollectLogEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = this.collectLogDao.selectExtList(queryModel);
        }

        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = collectLogDao.selectCount(queryModel);
        }

        CollectLogGetModel result = collectLogCustomConvertor.extList2collectLogGetModel(extList, total, null, query);
        if (!StringUtils.isEmpty(show)) {
            if ("one".equals(show)) {
                CollectLogModel collectLogModel = result.getModel();
                if (collectLogModel != null) {
                    String funcitonCode = collectLogModel.getFunctionCode();
                    Integer protocalId = collectLogModel.getProtocalId();
                    if (!StringUtils.isEmpty(funcitonCode) && protocalId != null) {
                        FunctionEntity functionEntity = FunctionEntityConstant.getFunctionEntityByProtocolIdAndFunctionCode(protocalId, funcitonCode);
                        if (functionEntity != null) {
                            Integer functionTmpId = functionEntity.getFunctiontmpId();
                            FunctiontmpEntity functiontmpEntity = FunctiontmpEntityConstant.getById(functionTmpId);
                            if (functiontmpEntity != null) {
                                collectLogModel.setFunctionName(functiontmpEntity.getName());
                            }
                        }

                    }
                }
            }
            if ("list".equals(show)) {
                List<CollectLogModel> collectLogModels = result.getModelList();
                for (CollectLogModel collectLogModel :
                collectLogModels) {
                    String funcitonCode = collectLogModel.getFunctionCode();
                    Integer protocalId = collectLogModel.getProtocalId();
                    if (!StringUtils.isEmpty(funcitonCode) && protocalId != null) {
                        FunctionEntity functionEntity = FunctionEntityConstant.getFunctionEntityByProtocolIdAndFunctionCode(protocalId, funcitonCode);
                        if (functionEntity != null) {
                            Integer functionTmpId = functionEntity.getFunctiontmpId();
                            FunctiontmpEntity functiontmpEntity = FunctiontmpEntityConstant.getById(functionTmpId);
                            if (functiontmpEntity != null) {
                                collectLogModel.setFunctionName(functiontmpEntity.getName());
                            }
                        }
                    }
                }
            }
        }
        return result;
    }
}



































































